from typing import Union, List, Optional

from fastapi import APIRouter, Depends, Body, Header, Query, Path, Form
from fastapi.encoders import jsonable_encoder
from odoo.api import Environment
from odoo.addons.fastapi.dependencies import odoo_env
from odoo.addons.fastapi_odoo_bridge.core.response import custom_response
from odoo.addons.fastapi_odoo_bridge.core.header import set_lang
from ...basemodels.dto.center_area.center_area import QuestionAndAnswerLevelOneFilterDto
from ...basemodels.vo.center_area.question_and_answer import QuestionAndAnswerKeyword, QuestionAndAnswerLevelOneBase, QuestionAndAnswerLevelOneDetail
from ...functions.center_area_func import CenterAreaFunc
from odoo.addons.fastapi_odoo_bridge.core.basemodel import CommonResponse

router = APIRouter(tags=['Q&A帮助中心'])


@router.get(
    path='/get/question_and_answer/keyword',
    summary='获取问答关键字',
    description='获取问答关键字',
    response_model=Union[list, dict, List[dict]]
)
@custom_response
@set_lang
async def get_question_and_answer_keyword(env: Environment = Depends(odoo_env), Accept_Language: Optional[str] = Header()):
    func = CenterAreaFunc(env)
    result = func.get_question_and_answer_keyword()
    if not isinstance(result, dict):
        result = [QuestionAndAnswerKeyword(__from_odoo=record, __env=env) for record in result]
        result = jsonable_encoder(CommonResponse(code='200', msg='success', data=result))
    return result


@router.post(
    path='/get/question_and_answer/level_one',
    summary='获取一级问答列表',
    description='获取一级问答列表',
    response_model=Union[list, dict, List[dict]]
)
@custom_response
@set_lang
async def get_question_and_answer_list(filter_params: QuestionAndAnswerLevelOneFilterDto = Body(..., description='筛选条件'), env: Environment = Depends(odoo_env), Accept_Language: Optional[str] = Header()):
    func = CenterAreaFunc(env)
    response = func.get_question_and_answer_list(filter_params)
    if not isinstance(response, dict):
        response = [QuestionAndAnswerLevelOneBase(__from_odoo=record, __env=env) for record in response]
        response = jsonable_encoder(CommonResponse(code='200', msg='success', data=response))
    return response


@router.get(
    path='/get_one/question_and_answer/detail',
    description='获取Q&A问答详情数据',
    summary='获取Q&A问答详情数据',
    response_model=Union[list, dict, List[dict]]
)
@custom_response
@set_lang
async def get_one_question_and_answer_detail(question_and_answer_id: int = Query(..., description='Q&A问答ID'), env: Environment = Depends(odoo_env),
                                             Accept_Language: Optional[str] = Header()):
    func = CenterAreaFunc(env)
    response = func.get_one_question_and_answer_detail(question_and_answer_id)
    if not isinstance(response, dict):
        response = QuestionAndAnswerLevelOneDetail(__from_odoo=response, __env=env)
        response = jsonable_encoder(CommonResponse(code='200', msg='success', data=response))
    return response
